<?php
namespace Core\Service;

use Phalcon\Di\Injectable;
use Core\Exception as CoreException;
use Core\Exception\Pretty as PrettyException;

class Environment extends Injectable {
    public function register() {
        $config = $this->config->get('system');
        set_error_handler(
            function ($errorCode, $errorMessage, $errorFile, $errorLine) {
                throw new \ErrorException($errorMessage, $errorCode, 1, $errorFile, $errorLine);
            }
        );
        set_exception_handler(
            function ($e) use ($config) {
                $errorId = CoreException::logException($e);
                if ($config->isConsole) {
                    echo 'Error <' . $errorId . '>: ' . $e->getMessage();
                    return true;
                }
                if ($config->debug) {
                    $exception = new PrettyException();
                    $exception->setBaseUri('assets/js/core/pretty-exceptions/');
                    return $exception->handleException($e);
                }
                return true;
            }
        );
        return $this;
    }
}